package com.eduhzy.ttw.work.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.work.R;
import com.eduhzy.ttw.work.R2;
import com.eduhzy.ttw.work.di.component.DaggerWorkHomeComponent;
import com.eduhzy.ttw.work.di.module.WorkHomeModule;
import com.eduhzy.ttw.work.mvp.contract.WorkHomeContract;
import com.eduhzy.ttw.work.mvp.presenter.WorkHomePresenter;
import com.eduhzy.ttw.work.mvp.ui.fragment.WorkHomeFragment;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-1 15:22:57
 */
@Route(path = RouterHub.WORK_WORKHOMEACTIVITY)
public class WorkHomeActivity extends BaseActivity<WorkHomePresenter> implements WorkHomeContract.View {
    @BindView(R2.id.tabs)
    TabLayout tabs;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;


    List<Fragment> mFragments;

    String[] mTitles = new String[]{
            "近七天", "历史作业"
    };
    @BindView(R2.id.public_toolbar_iv_right)
    ImageView mPublicToolbarIvRight;
    @BindView(R2.id.public_toolbar_right_iv_layout)
    AutoRelativeLayout mRightLayout;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerWorkHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .workHomeModule(new WorkHomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        MobclickAgent.onEvent(getActivity(), "ttw_work_home");
        return R.layout.activity_work_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRightLayout.setVisibility(View.VISIBLE);
        mPublicToolbarIvRight.setImageResource(R.drawable.public_icn_tianjia);
        mFragments = new ArrayList<>();
        WorkHomeFragment week = WorkHomeFragment.newInstance(1);
        mFragments.add(week);
        WorkHomeFragment history = WorkHomeFragment.newInstance(2);
        mFragments.add(history);
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getSupportFragmentManager(), Arrays.asList(mTitles), mFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(mTitles.length);
        tabs.setupWithViewPager(viewpager);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void setEmptyView() {

    }


    @OnClick(R2.id.public_toolbar_right_iv_layout)
    public void onViewClicked() {
        ARouter.getInstance().build(RouterHub.WORK_PUBLISHWORKACTIVITY)
                .withString(Intent.EXTRA_TITLE, Constants.PUBLISH_WORK)
                .navigation(getActivity());
    }
}
